package org.sherlock.ui.gui.view;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;

import org.sherlock.commons.download.FetcherException;
import org.sherlock.downloader.Download;
import org.sherlock.downloader.DownloadsNotifier;
import org.sherlock.downloader.fetchers.FetcherFactory;
import org.sherlock.ui.gui.components.JDownloadPanel;

public class MainWindow extends JFrame implements Observer {

	private static final long serialVersionUID = 1224321851920018212L;
	private DownloadsNotifier notifier;
	private ArrayList<JDownloadPanel> downloadPanels;
	private JScrollPane scrollPane;
	private JPanel downloadsPanel;

	public MainWindow() {
		setTitle("Sherlock Downloader");
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setSize(640, 480);
		getContentPane().setLayout(new BorderLayout());
		getContentPane().add(getScrollPane(), BorderLayout.CENTER);
		setVisible(true);
		downloadPanels = new ArrayList<JDownloadPanel>();
		notifier = new DownloadsNotifier();
	}

	public MainWindow subscribe() {
		notifier.addObserver(this);
		return this;
	}

	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane(getDownloadsPanel());
		}
		return scrollPane;
	}

	private JPanel getDownloadsPanel() {
		if (downloadsPanel == null) {
			downloadsPanel = new JPanel(new GridLayout(0, 1));
		}
		return downloadsPanel;
	}

	private void addDownload(final Download download) {
		SwingUtilities.invokeLater(new Runnable() {

			@Override
			public void run() {
				JDownloadPanel panel = new JDownloadPanel(download);
				downloadPanels.add(panel);
				downloadsPanel.add(panel);
				downloadsPanel.revalidate();
			}
		});
	}

	public void testStartDownload() {
		String localURL = "file://C:/Users/k1773n/Desktop/Download";
		String remoteURL = "http://www.jhepple.com/SampleMovies/AVI_to_MPEG-4.avi";
		Download download;
		Download download1;
		try {
			download = new Download(new URL(localURL), new URL(remoteURL));
			download1 = new Download(new URL(localURL + 1), new URL(remoteURL));
			FetcherFactory.getFetcher(download);
			FetcherFactory.getFetcher(download1);
			synchronized (download) {
				notifier.addDownload(download);
				notifier.addDownload(download1);
				addDownload(download);
				addDownload(download1);
			}
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FetcherException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public void update(Observable arg0, Object arg1) {
		ArrayList<Download> downloads = (ArrayList<Download>) arg1;
		for (JDownloadPanel panel : downloadPanels) {
			Download panelDownload = panel.getDownload();
			if (downloads.contains(panelDownload))
				panel.updateDownload();
		}
	}

}
